function changePoints(){
	var startPoint = $('driveLine_startname');
	var endPoint = $('driveLine_endname');
	var tempValue = startPoint.value;
	startPoint.value = endPoint.value;
	endPoint.value = tempValue;
}

/*驾车*/
function DriveSearch(){
	this.driveSType="rs";
	this.route_way = "0";
	this.point_id ="";
	this.start_x = "";
	this.start_y = "";
	this.start_name = "";
	this.start_address = "";
	this.start_tel = "";
	this.start_pid = "";
	this.start_citycode = "";
	this.start_type="";
	this.start_cityname = "";
	this.start_detailLink="";
	this.end_x = "";
	this.end_y = "";
	this.end_name = "";
	this.end_address = "";
	this.end_tel = "";
	this.end_pid = "";
	this.end_citycode = "";
	this.end_cityname = "";
	this.end_detailLink="";
	this.end_type="";
	this.route_segment;
}
var driveS = new DriveSearch();
function driveLineSearch(){
	driveS.start_cityname = $('driveLine_citynameS').value;
	driveS.end_cityname = $('driveLine_citynameE').value;
	driveS.start_name = $('driveLine_startname').value;
	driveS.end_name = $('driveLine_endname').value;
	var che = "";var che="";
	var i = 1 ;
	if(driveS.start_name=="请输入起点" || driveS.start_name==""){
		che += i + "．请输入起点\n" ;
		i = i + 1 ;
	}
	if(driveS.end_name=="" || driveS.end_name=="请输入终点"){
		che += i + "．请输入终点\n" ;
		i = i + 1 ;
	}
	if(driveS.start_cityname =="" ){
		che += i + "．请选择起点城市\n" ;
		i = i + 1 ;
	}
	if(driveS.end_cityname =="" ){
		che += i + "．请选择终点城市\n" ;
		i = i + 1 ;
	}
	if(i==1){
		driveS.driveSType = "rs";
		driveLine_Startsearch(driveS.start_cityname,driveS.start_name);
	}else{
		alert(che);
	}
}
function driveLine_Startsearch(c,s){
	if (typeof MLocalSearch == "undefined"){
		parent.showDialog('error','数据查询服务连接异常',2);
		return;
	}
	var mls =new MLocalSearch();
	var mlsp= new MLocalSearchOptions();
	mlsp.recordsPerPage=1;
	mls.setCallbackFunction(driveLineStartsearch_CallBack);
	mls.poiSearchByKeywords(s,c,mlsp);
}
function driveLineStartsearch_CallBack(data){
	if(driveS.driveSType=="rs"){
		driveS.start_x = data.poilist[0].x;
		driveS.start_y = data.poilist[0].y;
		driveS.start_name = data.poilist[0].name;
		driveS.start_address = data.poilist[0].address;
		driveS.start_tel = data.poilist[0].tel;
		driveS.start_type = data.poilist[0].type;
		driveS.start_pid = data.poilist[0].pguid;
		driveS.start_citycode = data.poilist[0].citycode;
	}
	if(driveS.driveSType=="re"){
		driveS.end_x = data.poilist[0].x;
		driveS.end_y = data.poilist[0].y;
		driveS.end_name = data.poilist[0].name;
		driveS.end_address = data.poilist[0].address;
		driveS.end_tel = data.poilist[0].tel;
		driveS.end_type = data.poilist[0].type;
		driveS.end_pid = data.poilist[0].pguid;
		driveS.end_citycode = data.poilist[0].citycode;
	}
	driveLine_Endsearch();
}
function driveLine_Endsearch(){
	if(driveS.driveSType=="rs"){
		driveS.driveSType = "re";
		driveLine_Startsearch(driveS.end_cityname,driveS.end_name);
	}else{driveLine_search();}
}

/* 地图画线 */
function driveLine_search(){
	if(driveS.start_x=="" || driveS.start_y=="" || driveS.end_x=="" || driveS.end_y=="" ){
		alert("起点和终点坐标不能为空!");
	}else{
		var startXY= new MLngLat(driveS.start_x,driveS.start_y);
		var endXY = new MLngLat(driveS.end_x,driveS.end_y);
		var mrs =new MRoutSearch();
		var option =new MRoutSearchOptions();
		mrs.setCallbackFunction(driveLineSearch_CallBack_n);
		mrs.routSearchByStartXYAndEndXY(MRoutSearch.DriveSearch,startXY,endXY,driveS.start_name,option);
	}
}

function driveLineSearch_CallBack_n(data){
	var route_count = data.count;
	if (route_count==0) {
		alert("未查询到任何结果");
	} else {
		drawline(data.coors,driveS.start_x,driveS.start_y);
	}

}

function drawline(coors,startX,startY){ //画驾车路线方法   
	mapObj.removeAllOverlays();
	var arrline = new Array();   
	var linexy =coors.split(',');   
	var line_l = (linexy.length-1)/2;   
	for(var i=0;i<line_l;i++){   
		arrline.push(new MLngLat(linexy[2*i],linexy[2*i+1]));   
	}   
	var lineS=new MLineStyle();   
	lineS.thickness = 3;   
	lineS.color = 0xff230b;   
	lineS.alpha = 1;   
	var lineoption  = new MLineOptions();   
	lineoption.lineStyle = lineS;   
	var line = new MPolyline(arrline,lineoption);   
	mapObj.addOverlay(line,true);   
	var lnglat = new MLngLat( startX, startY);   
	var markerOption = new MMarkerOptions();   
	markerOption.imageUrl ="http://code.mapabc.com/images/car_03.png";//   
	var Mmarker1 = new MMarker(lnglat,markerOption);   
	Mmarker1.id="bus1";   
	mapObj.addOverlay(Mmarker1);   
	mapObj.markerMoveAlong("bus1",arrline,3);   
	mapObj.startMoveAlong('bus1',true);   
}

function driveLineSearch_CallBack(data){
	$('route-div-1').style.display="none";
	$('route-div-2').style.display="none";
	$('routeResult-div-1').style.display="block";
	$('routeResult-div-2').style.display="block";
	switch(data.message){
	case 'ok':
		var route_count = data.count;
		if(route_count==0){
			$('routeResultInner').innerHTML="<div>&nbsp;&nbsp;&nbsp;&nbsp;未查找到任何结果!</div><div>&nbsp;&nbsp;&nbsp;&nbsp;建议：</div><div>&nbsp;&nbsp;&nbsp;&nbsp;1.请确保所有字词拼写正确。</div><div>&nbsp;&nbsp;&nbsp;&nbsp;2.尝试不同的关键字。</div><div>&nbsp;&nbsp;&nbsp;&nbsp;3.尝试更宽泛的关键字。</div>";
		}else{
			var route_text = "";var road_length = 0;
			var route_content = new Array();
			driveS.route_segment = new Array();
			for(var i=0;i<route_count;i++){
				driveS.route_segment[i] = data.segmengList[i].coor;//每一条路线的XY
				road_length += parseInt(data.segmengList[i].roadLength);//每一条路线的里程
				if(i==0){
					route_text += "<tr class=\"td_style\" id=\"tr_"+i+"\" onMouseover=\"className='td_style1'\" onMouseout=\"className='td_style'\" onclick=\"driveLineDrawFoldline('"+i+"','"+route_count+"')\"><td align=\"center\">"+(i+1)+".</td><td width=\"177\">沿<strong>"+data.segmengList[i].roadName+"</strong>向<strong>"+data.segmengList[i].direction+"</strong><span class=\"gray\">行驶</span></td><td align=\"right\">"+Getdistance(data.segmengList[i].roadLength)+"</td><td>&nbsp;</td></tr>";
				}else{
					route_text += "<tr class=\"td_style\" id=\"tr_"+i+"\" onMouseover=\"className='td_style1'\" onMouseout=\"className='td_style'\" onclick=\"driveLineDrawFoldline('"+i+"','"+route_count+"')\"><td align=\"center\">"+(i+1)+".</td><td width=\"177\">"+data.segmengList[i-1].action+"<span class=\"gray\">进入</span><strong>"+data.segmengList[i].roadName+"</strong>向<strong>"+data.segmengList[i].direction+"</strong><span class=\"gray\">行驶</span></td><td align=\"right\">"+Getdistance(data.segmengList[i].roadLength)+"</td><td>&nbsp;</td></tr>";
				}
			}
			route_content.push("<table width=\"309\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td colspan=\"2\" style=\"background:#e1e1e1;\">　　路线</td><td width=\"82\" style=\" border-left:1px solid #fff; background:#e1e1e1;\">　　　里程</td><td width=\"17\" style=\"background:#e1e1e1;\">&nbsp;</td> </tr><tr><td width=\"33\" align=\"center\"><img src=\"images/start.gif\" /></td><td colspan=\"2\">起点："+driveS.start_name+"</td><td>&nbsp;</td></tr>"+route_text+"<tr><td width=\"33\" align=\"center\"><img src=\"images/end.gif\" /></td><td colspan=\"2\">终点："+driveS.end_name+"</td></tr><tr><td colspan=\"3\" align=\"right\">共："+Getdistance(road_length)+"</td></tr></table>");
			$('routeResultInner').innerHTML=route_content.join("");

			var allover = new Array();
			var arr = new Array();
			var poi_xy_r = data.coors.split(",");
			var poi_xy_rl = poi_xy_r.length-2
			for(var j=0;j<poi_xy_rl;j=j+2){
				arr.push(new MLngLat(poi_xy_r[j],poi_xy_r[j+1]));
			}
			var lineopt = new MLineOptions();
			lineopt.lineStyle.thickness=6;
			lineopt.lineStyle.color=0x005cb5;
			lineopt.lineStyle.alpha=0.8;
			var line = new MPolyline(arr,lineopt);
			lineopt.canShowTip=false;
			allover.push(line);
			
			/*起点终点*/
			var lineStyle=new MLineStyle();
			lineStyle.thickness=2;
			lineStyle.alpha=1;
			var fontStyle=new MFontStyle();
			fontStyle.name="Arial";
			fontStyle.size=12;
			fontStyle.color= 0xffffff;
			fontStyle.bold=true;
			var fontStyle1=new MFontStyle();
			fontStyle1.name="Arial";
			fontStyle1.size=13;
			fontStyle1.color= 0x000000;
			fontStyle1.bold=false;
			var fillStyle=new MFillStyle();
			fillStyle.color= 0xFFFFFF; //填充色
			fillStyle.alpha=1;
			var fillStyle1=new MFillStyle();
			fillStyle1.color= 0x005cb5; //填充色
			fillStyle1.alpha=1;
			var startmarkerOption = new MMarkerOptions();
			startmarkerOption.imageUrl ="http://code.mapabc.com/images/qd.png";
			startmarkerOption.picAgent = false;
			startmarkerOption.isDraggable=false;//是否可以拖动
			var tipOption = new MTipOptions();
			tipOption.title=driveS.start_name;
			var tipC = TipContents(driveS.start_type,driveS.start_address,driveS.start_tel)
			tipOption.content=tipC;//tip内容
			tipOption.hasShadow=true;
			tipOption.borderStyle=lineStyle;
			tipOption.titleFontStyle=fontStyle;
			tipOption.contentFontStyle=fontStyle1;
			tipOption.fillStyle=fillStyle;
			tipOption.titleFillStyle=fillStyle1;
			startmarkerOption.tipOption = tipOption;
			var start = new MLngLat(driveS.start_x,driveS.start_y);
			var startmarker= new MMarker(start,startmarkerOption);
			startmarker.id=driveS.start_pid;
			allover.push(startmarker);

			var endmarkerOption = new MMarkerOptions();
			endmarkerOption.imageUrl ="http://code.mapabc.com/images/zd.png";
			endmarkerOption.picAgent = false;
			endmarkerOption.isDraggable=false;//是否可以拖动
			var tipOption = new MTipOptions();
			tipOption.title=driveS.end_name;
			var tipC = TipContents(driveS.start_type,driveS.start_address,driveS.start_tel)
			tipOption.content=tipC;//tip内容
			tipOption.hasShadow=true;
			tipOption.borderStyle=lineStyle;
			tipOption.titleFontStyle=fontStyle;
			tipOption.contentFontStyle=fontStyle1;
			tipOption.fillStyle=fillStyle;
			tipOption.titleFillStyle=fillStyle1;
			endmarkerOption.tipOption = tipOption;
			var end = new MLngLat(driveS.end_x,driveS.end_y);
			var endmarker= new MMarker(end,endmarkerOption);
			endmarker.id=busS.end_pid;
			allover.push(endmarker);
			mapObj.addOverlays(allover,true);
		
			/*添加小车*/
			var lnglat = new MLngLat(poi_xy_r[0],poi_xy_r[1]);
			var busmarkerOption = new MMarkerOptions();
			busmarkerOption.imageUrl ="http://code.mapabc.com/images/car_03.png";
			busmarkerOption.picAgent = false;
			busmarkerOption.imageAlign=5;
			var busmarker = new MMarker(lnglat,busmarkerOption);
			busmarker.id="driveRoute";
			mapObj.addOverlay(busmarker);
	
			//mapObj.setGPSFocus("bus1",0);//设置标注为焦点模式
	
			//使图标在第一段路线移动
			mapObj.markerMoveAlong("route1",arr);
			mapObj.startMoveAlong('route1',true);
		}
	break;
	case 'error':
		$("routeResultInner").innerHTML = "<div class=\"default\"><div class=\"default_title\">网络忙！请重新尝试！</div><div class=\"d_link\"><div class=\"d_right\"></div><div class=\"suggest\"><strong>建议：</strong><br />如果您刷新页后仍无法显示结果，请过几分钟后再次尝试或者与我们的服务人员联系。<br />Email：service@mapabc.com <br />电话：400 810 0080</div></div></span>错误信息："+data.message+"</div>";
	break;
	default:
		$("routeResultInner").innerHTML = "<div class=\"default\"><div class=\"default_title\">对不起！网络繁忙！请稍后重新尝试！</div><div class=\"d_link\"><div class=\"d_right\"></div><div class=\"suggest\"><strong>建议：</strong><br />如果您刷新页后仍无法显示结果，请过几分钟后再次尝试或者与我们的服务人员联系。<br />Email：service@mapabc.com <br />电话：400 810 0080</div></div></span>错误信息："+data.message+"</div>";
	}
}

function driveLineDrawFoldline(num,count){//画线路并控制左边列表.num为第几条线路,count全部线路数.
	try{
		var tr_id = "tr_"+num;
		for(var i=0;i<count;i++){
			var id = "tr_"+i;
			$(id).style.backgroundColor='#fff';
			$(id).onmouseout = function(){this.style.backgroundColor='#fff';};
		}
		$(tr_id).style.backgroundColor='#efefef';
		$(tr_id).onmouseout = function(){this.style.backgroundColor='#efefef';};

		var lineopt = new MLineOptions();
		lineopt.lineStyle.thickness=6;
		lineopt.lineStyle.color=0x33CC00;
		lineopt.lineStyle.alpha=0.8;

		var arr = new Array();
		var poi_xy  = driveS.route_segment[num].split(",");
		var l = poi_xy.length-1;
		for(var e=0;e<l;e=e+2){
			arr.push(new MLngLat(poi_xy[e],poi_xy[e+1]));
		}
		var line = new MPolyline(arr,lineopt);
		line.id="driveline";
		mapObj.addOverlay(line,true);
	}catch(e){}
}
function removeAllOverlays(){
	mapObj.removeAllOverlays();
}
function RegMatch(inputStr){
	var patrn = /^[A-Za-z0-9]|[._]+$/;
	if (!patrn.exec(inputStr)) return false
	return true
}
function RegMatch1(inputStr){
	var patrn=/^[0-9]+$/;
	if (!patrn.exec(inputStr)) return false
	return true
}
